﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Xml;
using BusinessLogic.ClassFactory;
using DataAccess;
using Model.Data;

namespace BusinessLogic.BusinessRules
{
    /// <summary>
    /// Comment 的摘要说明。
    /// </summary>
    public class Comment : BaseClass
    {
        #region 必有属性
        public Model.Data.CommentData.CommentRow DataItem
        {
            get
            {
                return (Model.Data.CommentData.CommentRow)base.m_CurrentRow;
            }
            set
            {
                base.m_CurrentRow = value;
            }
        }

        private Model.Data.CommentData m_Data
        {
            get
            {
                return (Model.Data.CommentData)base.m_DataSet;
            }
            set
            {
                base.m_DataSet = value;
            }
        }


        public override int Count
        {
            get
            {
                return this.m_Data.Comment.Count;
            }
        }
        #endregion

        #region 构造函数
        public Comment(DataAccess.DBManager m_DBManager, DataAccess.BaseAccess m_Access)
            : base(m_DBManager, m_Access)
        {
        }
        #endregion

        #region 必须重载方法
        /// <summary>
        /// 根据数据集中索引,设置当前数据行
        /// </summary>
        /// <param name="n_index">数据集中的索引</param>
        public override void SelectRow(int n_index)
        {

            if (m_Data != null && m_Data.Comment.Rows.Count > n_index)
            {
                DataItem = m_Data.Comment[n_index];
            }
        }


        /// <summary>
        /// 实现更新
        /// </summary>
        /// <returns></returns>
        protected override int OnUpdate()
        {
            return m_Access.Update(this.m_DataSet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        protected override bool OnDelete(int ID)
        {
            return true;
        }


        public override void NewDataSet()
        {

            this.m_Data = new CommentData();
            m_CurrentRow = m_Data.Comment.NewCommentRow();
        }

        public override void SetDataSet(DataSet m_DataSet)
        {
            this.m_DataSet = m_DataSet;
        }

        public override bool OnInsert()
        {
            this.DataItem.intCommentID = this.GetNextSeed();
            this.m_Data.Comment.Rows.Add(this.m_CurrentRow);
            return true;
        }
        #endregion

        #region 业务属性
        public Model.Data.CommentData Data
        {
            get
            {
                return (Model.Data.CommentData)base.m_DataSet;
            }
            set
            {
                base.m_DataSet = value;
            }
        }

        private static string GetCommentIssueFile
        {
            get
            {
                StringBuilder strSqlSegment = new StringBuilder();//自定义sql片段
                string[] aryFields = new string[17] { "intCommentID", "chvTitle", "chvSubTitle", "chvSummary", "chvDescription", "chvKeyWords", "txtContent", "chvMainPic", "dtmInTime", "chvCommentInMan", "intAccessCount", "intType", "chvFilePath", "chvVedioPath", "txtSeoTitle", "txtSeoKeywords", "txtSeodescription" };
                strSqlSegment.Append("Select ");
                for (int i = 0; i < aryFields.Length; i++)
                {
                    if (i == aryFields.Length - 1)
                    {
                        strSqlSegment.Append(aryFields[i] + "  ");
                    }
                    else
                    {
                        strSqlSegment.Append(aryFields[i] + ",");
                    }
                }
                strSqlSegment.Append(" From [Comment] Where intCommentID=");
                return strSqlSegment.ToString();
            }
        }
        ///// <summary>
        ///// 
        ///// </summary>
        //private static string GetCommentIssueNoContentFile
        //{
        //    get
        //    {
        //        StringBuilder strSqlSegment = new StringBuilder();//自定义sql片段
        //        string[] aryFields = new string[17] { "intCommentID", "chvSubTitle", "chvTitle", "chvDescription", "chvKeyWords", "chvMainPic", "txtContent", "dtmInTime", "chvCommentInMan", "intAccessCount", "chvMainMiniPic", "chvSummary", "chvFilePath", "chvVedioPath", "txtSeoTitle", "txtSeoKeywords", "txtSeodescription" };
        //        strSqlSegment.Append("Select ");
        //        for (int i = 0; i < aryFields.Length; i++)
        //        {
        //            if (i == aryFields.Length - 1)
        //            {
        //                strSqlSegment.Append(aryFields[i] + "  ");
        //            }
        //            else
        //            {
        //                strSqlSegment.Append(aryFields[i] + ",");
        //            }
        //        }
        //        strSqlSegment.Append(" From [Comment] Where intCommentID in");
        //        return strSqlSegment.ToString();
        //    }
        //}
        #endregion
        ////发布文章字段，处理type=4的插件
        //public string GetCommentHavecontentXml(int intSTID)
        //{
        //    string strSql = GetCommentIssueFile + this.DataItem.intCommentID;
        //    DataTable tblTmp = this.m_Access.ExecuteDataTable(strSql);

        //    // 将某些字段属性值进行修改
        //    DataRow row = tblTmp.Rows[0];

        //    //// 修改文章图片链接
        //    //if (int.Parse(row["intType"].ToString()) == 3)//flash
        //    //{
        //    //    StringBuilder strContent = new StringBuilder();
        //    //    strContent.AppendFormat("<OBJECT classid=\"clsid: D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" WIDTH=\"{0}\" HEIGHT=\"550\">", System.Configuration.ConfigurationSettings.AppSettings["strFlashWidth"].ToString());
        //    //    strContent.AppendFormat("<PARAM NAME=\"movie\" VALUE={0}>", SystemVariable.WebUrl + "Pictures/" + row["txtContent"].ToString());
        //    //    strContent.Append("<param name=\"wmode\" value=\"transparent\">");
        //    //    strContent.Append("<PARAM NAME=\"quality\" VALUE=\"transparent\">");
        //    //    strContent.Append("<PARAM NAME=\"bgcolor\" VALUE=\"#E0F8E2\">");
        //    //    strContent.AppendFormat("<EMBED wmode=\"transparent\" src={0} quality=\"high\" bgcolor=\"#E0F8E2\" WIDTH=\"{1}\" HEIGHT=\"550\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></EMBED></OBJECT>",
        //    //        SystemVariable.WebUrl + "Pictures/" + row["txtContent"].ToString(), 
        //    //        System.Configuration.ConfigurationSettings.AppSettings["strFlashWidth"].ToString());
        //    //    row["txtContent"] = strContent.ToString();
        //    //}

        //    //if (int.Parse(row["intType"].ToString()) == 4)
        //    //{
        //    //    StringBuilder strContent = new StringBuilder();
        //    //    strContent.Append("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"490\" height=\"430\"> ");
        //    //    strContent.AppendFormat("<param name=\"movie\" value=\"Flvplayer.swf\" /> <param name=\"quality\" value=\"high\" /> <param name=\"wmode\" value=\"opaque\" /> <param name=\"allowFullScreen\" value=\"true\" /> ");
        //    //    strContent.AppendFormat("<param name='FlashVars' value='vcastr_file={0}' /> ", SystemVariable.WebUrl + "Pictures/" + row["txtContent"].ToString());
        //    //    strContent.AppendFormat("<embed src='Flvplayer.swf' allowfullscreen='true' flashvars='vcastr_file={0}'  quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='100%' height='100%'></embed></object>", SystemVariable.WebUrl + "Pictures/" + row["txtContent"].ToString());
        //    //    row["txtContent"] = strContent.ToString();
        //    //}


            //#region 生成XML
            //StringBuilder sbXML = new StringBuilder();
            //XmlTextWriter objWriter = new XmlTextWriter(new System.IO.StringWriter(sbXML));
            //try
            //{
            //    objWriter.WriteStartDocument();
            //    objWriter.WriteStartElement("TMPL");
            //    // 修改文章图片链接
            //    if (row["chvMainPic"] != DBNull.Value)
            //    {
            //        row["chvMainPic"] = SystemVariable.WebUrl + "Pcfiles/" + row["chvMainPic"].ToString();
            //    }
            //    else
            //        row["chvMainPic"] = SystemVariable.WebUrl + "/Pcfiles/Mini/Default.jpg";
            //    objWriter.WriteStartElement("Comment");

            //    // 修改文章附件链接
            //    if (row["chvFilePath"] != DBNull.Value)
            //    {
            //        if (row["chvFilePath"].ToString().Trim() != "")
            //        {
            //            row["chvFilePath"] = SystemVariable.WebUrl + "Pcfiles/" + row["chvFilePath"].ToString();
            //        }
            //    }
            //    // 修改文章视频链接
            //    if (row["chvVedioPath"] != DBNull.Value)
            //    {
            //        if (row["chvVedioPath"].ToString().Trim() != "")
            //        {
            //            row["chvVedioPath"] = SystemVariable.WebUrl + "Pcfiles/" + row["chvVedioPath"].ToString();
            //        }
            //    }

            //    // 添加数据库字段值
            //    for (int i = 0; i < tblTmp.Columns.Count; i++)
            //    {
            //        if (row[i] != DBNull.Value)
            //        {
            //            if (tblTmp.Columns[i].ColumnName == "dtmInTime")
            //            {
            //                DateTime dtmInDate = DateTime.Parse(row["dtmInTime"].ToString());
            //                objWriter.WriteStartElement(tblTmp.Columns[i].ColumnName);
            //                objWriter.WriteAttributeString("year", dtmInDate.Year.ToString("000#"));
            //                objWriter.WriteAttributeString("month", dtmInDate.Month.ToString("0#"));
            //                objWriter.WriteAttributeString("day", dtmInDate.Day.ToString("0#"));
            //                objWriter.WriteAttributeString("hh", dtmInDate.Hour.ToString("0#"));
            //                objWriter.WriteAttributeString("mm", dtmInDate.Minute.ToString("0#"));
            //                objWriter.WriteCData(dtmInDate.ToString("yyyy年MM月dd日"));
            //                objWriter.WriteEndElement();
            //            }
            //            else
            //            {
            //                objWriter.WriteStartElement(tblTmp.Columns[i].ColumnName);
            //                objWriter.WriteCData(row[i].ToString());
            //                objWriter.WriteEndElement();
            //            }


            //            //构建没有html字符的文章简介
            //            if (tblTmp.Columns[i].ColumnName == "chvSummary")
            //            {
            //                objWriter.WriteStartElement("chvNoHtmlSummary");
            //                objWriter.WriteCData(ClearKeyWord.ClearHTML(row[i].ToString()));
            //                objWriter.WriteEndElement();
            //            }

            //        }
            //    }
            //    // 文章当前位置	
            //    SectionsFactory objFactory = new SectionsFactory(this.m_DBManger);

            //    Sections objSection = objFactory.Creator(intSTID);
            //    objSection.GetCurPosXml(objWriter);


            //    objWriter.WriteEndElement();
            //    objWriter.WriteEndElement();
            //    objWriter.WriteEndDocument();

            //    objWriter.Flush();
            //}
            //finally
            //{
            //    objWriter.Close();
            //}


        //    #endregion


        //    return sbXML.ToString();
        //}
        /// <summary>
        /// 设置文章发布状态为已发布
        /// </summary>
        /// <param name="intSTID">栏目ID</param>
        /// <param name="IsIssue">是否已发布</param>
        public void SetCommentIssueStatus(int intSTID, bool IsIssue)
        {
            if (IsIssue)
            {
                SectionsFactory objFactory = new SectionsFactory(m_DBManger);
                objFactory.UpdateState(intSTID);
            }
        }
        ///// <summary>
        ///// 获取多篇文章的XML数据
        ///// </summary>
        ///// <param name="intSTID">栏目ID</param>
        ///// <param name="objWriter"></param>
        ///// <param name="aryCommentIDs">多篇文章的ID数组</param>
        ///// <param name="strSectionRealUrl">栏目的真实URL：暂时没有用处</param>
        //public void GetManyCommentsNoContentXml(XmlTextWriter objWriter, ArrayList aryCommentIDs)
        //{
        //    if (aryCommentIDs == null)
        //    {
        //        return;
        //    }
        //    string strIDs = "(";
        //    for (int i = 0; i < aryCommentIDs.Count; i++)
        //    {
        //        strIDs += string.Format("{0}, ", aryCommentIDs[i]);
        //    }
        //    strIDs += "-1000)";		// 添加一个不存在的CommentID,以便组成格式(1,2,3,)

        //    string strSql = GetCommentIssueNoContentFile + strIDs + " order by dtmInTime";
        //    DataTable tblTmp = this.m_Access.ExecuteDataTable(strSql);

        //    //必须按照aryCommentIDs的顺序来组织
        //    for (int x = 0; x < aryCommentIDs.Count; x++)
        //    {
        //        // 将某些字段属性值进行修改
        //        string strFilter = string.Format("intCommentID = {0}", aryCommentIDs[x]);
        //        DataRow row = tblTmp.Select(strFilter)[0];

        //        // 修改文章图片链接
        //        if (row["chvMainPic"] != DBNull.Value)
        //        {
        //            row["chvMainPic"] = SystemVariable.WebUrl + "Pcfiles/" + row["chvMainPic"].ToString();
        //        }
        //        else
        //            row["chvMainPic"] = SystemVariable.WebUrl + "/Pcfiles/Mini/Default.jpg";
        //        // 修改文章附件链接
        //        if (row["chvFilePath"] != DBNull.Value)
        //        {
        //            if (row["chvFilePath"].ToString().Trim() != "")
        //            {
        //                row["chvFilePath"] = SystemVariable.WebUrl + "Pcfiles/" + row["chvFilePath"].ToString();
        //            }
        //        }
        //        // 修改文章视频链接
        //        if (row["chvVedioPath"] != DBNull.Value)
        //        {
        //            if (row["chvVedioPath"].ToString().Trim() != "")
        //            {
        //                row["chvVedioPath"] = SystemVariable.WebUrl + "Pcfiles/" + row["chvVedioPath"].ToString();
        //            }
        //        }

        //        #region 生成XML
        //        objWriter.WriteStartElement("Comment");

        //        // 添加数据库字段值
        //        for (int i = 0; i < tblTmp.Columns.Count; i++)
        //        {
        //            if (row[i] != DBNull.Value)
        //            {
        //                // 文章录入时间
        //                if (tblTmp.Columns[i].ColumnName == "dtmInTime")
        //                {
        //                    DateTime dtmInDate = DateTime.Parse(row["dtmInTime"].ToString());
        //                    objWriter.WriteStartElement(tblTmp.Columns[i].ColumnName);
        //                    objWriter.WriteAttributeString("year", dtmInDate.Year.ToString("000#"));
        //                    objWriter.WriteAttributeString("month", dtmInDate.Month.ToString("0#"));
        //                    objWriter.WriteAttributeString("day", dtmInDate.Day.ToString("0#"));
        //                    objWriter.WriteCData(dtmInDate.ToString("yyyy年MM月dd日"));
        //                    objWriter.WriteEndElement();
        //                }
        //                // 文章标题
        //                else if (tblTmp.Columns[i].ColumnName == "chvTitle")
        //                {
        //                    string strDisplayTitle = string.Empty; // 不含html格式的完整的标题 
        //                    string strColor = string.Empty;		// 标题颜色
        //                    string strStyle = string.Empty;		// 标题样式
        //                    string strSize = string.Empty;		// 标题字体大小
        //                    strDisplayTitle = row["chvTitle"].ToString();
        //                    objWriter.WriteStartElement(tblTmp.Columns[i].ColumnName);

        //                    objWriter.WriteCData(strDisplayTitle);
        //                    objWriter.WriteEndElement();
        //                }
        //                else if (tblTmp.Columns[i].ColumnName == "chvMainMiniPic")
        //                { }
        //                else
        //                {
        //                    objWriter.WriteStartElement(tblTmp.Columns[i].ColumnName);
        //                    objWriter.WriteCData(row[i].ToString());
        //                    objWriter.WriteEndElement();
        //                }
        //                //构建没有html字符的文章简介
        //                if (tblTmp.Columns[i].ColumnName == "chvSummary")
        //                {
        //                    objWriter.WriteStartElement("chvNoHtmlSummary");
        //                    objWriter.WriteCData(ClearKeyWord.ClearHTML(row[i].ToString()));
        //                    objWriter.WriteEndElement();
        //                }
        //            }
        //        }
        //        //文章缩略图连接
        //        objWriter.WriteStartElement("chvMainMiniPic");
        //        if (row["chvMainMiniPic"] != DBNull.Value)
        //            objWriter.WriteCData(SystemVariable.WebUrl + "Pcfiles/" + row["chvMainMiniPic"].ToString());
        //        else
        //            objWriter.WriteCData(SystemVariable.WebUrl + "/Pcfiles/Default.jpg");
        //        objWriter.WriteEndElement();
        //        // 文章URL
        //        string strCommentUrl = string.Empty;
        //        strCommentUrl = SystemVariable.WebUrl + "news/ahtml/" + "A__" + row["intCommentID"].ToString() + "." + Common.AppConfig.GetWebFileType();

        //        objWriter.WriteStartElement("chvFullURL");
        //        objWriter.WriteCData(strCommentUrl);
        //        objWriter.WriteEndElement();



        //        objWriter.WriteEndElement();
        //    }

        //        #endregion
        //}
    }
}
